package com.wh.common.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.common.service.ITaskService;
import com.wh.common.pojo.Task;
import com.wh.common.dto.TaskQueryDto;
import com.wh.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    public ITaskService taskService;

    /**
     * 接口：添加或修改
     *
     * @param task 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(sn = "task/addOrUpdate",name = "添加或修改任务")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Task task) {
        if (task.getTaskId() != null) {
            taskService.updateData(task);
        } else {
            taskService.insert(task);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param taskId
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "task/del",name = "根据id删除任务")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Integer taskId) {
        taskService.deleByid(taskId);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "task/patchDel",name = "批量删除任务")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        taskService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */

    @PreAuthorize(sn = "task/findOne",name = "根据id查询任务")
    @GetMapping("/{id}")
    public Task findOne(@PathVariable("id") Long id) {
        return taskService.getById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */

    @PreAuthorize(sn = "task/findAllTaskInfo",name = "查询全部任务信息")
    @GetMapping("/findAllTaskInfo")
    public AjaxResult findAll() {
        return AjaxResult.me().setResultObj(taskService.findall());
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param taskQueryDto 查询对象
     * @return IPage<Task> 分页对象
     */

    @PreAuthorize(sn = "task/findByPage",name = "分页查找任务")
    @PostMapping
    public IPage<Task> findByPage(@RequestBody TaskQueryDto taskQueryDto) {
        return taskService.findByPage(taskQueryDto);
    }
    @GetMapping("/taskall")
    public List<Task> findall(){
        return taskService.findall();
    }
}
